#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/select.h>
#include<mysql.h>
#include<string.h>
#include <pthread.h>

pthread_t tida,tidb;

int connectfd;

int nflag=-1;

struct password
{
	char id[20];
	char passwd[20];
	int flag;
};

struct password pw;

char buf[100],buff[20];

void *thread_function_Get(void *arg)
{
	while(1)
	{
		memset(buf,'\0',sizeof(buf));
		recv(connectfd,buf,sizeof(buf),0);
		printf("%s\n",buf);
	}
}

void *thread_function_Send(void *arg)
{
	while(1)
	{
		memset(pw.passwd,0,sizeof(pw.passwd));
		printf("请输入您要传输的数据:");
		int buff[100];
		scanf("%s",buff);
		sprintf(pw.passwd,"%s:%s",pw.id,buff);
		send(connectfd,&pw,sizeof(pw),0);
		if(strcmp(buff,"quit")==0)
		{
			return;
		}
	}
}

void group_chat(int fd)
{
	connectfd=fd;
	pw.flag=4;
	printf("请输入您的id:");
	scanf("%s",pw.id);
	send(connectfd,&pw,sizeof(pw),0);
        if(pthread_create(&tida,NULL,thread_function_Get,NULL) != 0)
        {
            perror("pthreada_create");
        }
        if(pthread_create(&tidb,NULL,thread_function_Send,NULL) != 0)
        {
            perror("pthreadb_create");
        }
        pthread_join(tida, NULL);
        pthread_join(tidb, NULL);
}
